import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.ArrayList; 
import oracle.jdbc.pool.OracleDataSource;
//getAgents displays the agents stored in the database in an html table
//getAgents also allows for the adding and deleting of agents
public class getAgents extends HttpServlet
{
	public static dbapi conn;
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println(htmlHeader.header);
		conn = new dbapi();
		//connect to Oracle
		conn.connect();
		if(!conn.connected)//if the connection does not work, inform the user w/ an error message
		{
			out.println("SQL ERROR CANNOT CONNECT TO DATABASE!<br>");
			out.println(conn.msg + "<br>");
			return;
		}
		ArrayList<String> agentList = conn.getAgents();//create an array list of agents
		//if the list is null, inform the user w/ an error message
		if(agentList == null){
			out.println("Error: null resultset from database<br>"+conn.msg);
			return;	
		//else do the following
		}else{
			//create an html agents table, displaying Agent ID and name
			out.println("<p style=\"font-size:20pt;\">Agents</p>"+
						"<table align=center width=\"500\" border=1>");
			out.println("<tr><th width=\"25%\">Agent ID</th><th>Name</th><th></th></tr>");
			//output all agents into this table
			for(int i=0;i<agentList.size();i += 2)
			{
				out.println("<tr><td>");
				out.println(agentList.get(i));
				out.println("</td><td>");
				out.println(agentList.get(i+1));
				out.println("</td><td>");
				out.println("<a href=\"../project/delAgent?id="+(agentList.get(i))+"\">Delete Agent</a></td>");
				out.println("</tr>");
			}	
			out.println("</table>");
		}
	}
}


